package com.jason.kotlin.meta.architecture2.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModelProvider
import com.jason.kotlin.meta.R
import java.lang.reflect.ParameterizedType

abstract class WanMvvmBaseActivity<T : AndroidViewModel> : WanBaseActivity() {

    protected lateinit var viewModel: T

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initViewModel()
    }

    fun setTitle(text: String) {
        toolbar!!.title = text
    }



    private fun initViewModel() {
        val modelClass: Class<*>
        val type = javaClass.genericSuperclass
        modelClass = if (type is ParameterizedType) {
            type.actualTypeArguments[0] as Class<*>
        }else{
            AndroidViewModel::class.java
        }


        //viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(
        //    modelClass)
        //viewModel = ViewModelProviders.of(this).get(modelClass)
    }

    private fun <T : AndroidViewModel?> create(modelClass: Class<T>) {
        ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(
            modelClass)

    }
}